﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnitySheet;
using UnityEngine.UI;

/// <summary>
/// 选项管理类
/// </summary>
public class OptionsManager : MonoBehaviour {
    /// <summary>
    /// 一级选项UI
    /// </summary>
    public ContentOptionGroupUI optionGroupListUI_I;
    /// <summary>
    /// 二级选项UI
    /// </summary>
    public ContentOptionGroupUI optionGroupListUI_II;

    /// <summary>
    /// 一级选项
    /// </summary>
    public OptionContent_Option selectedOption_I
    {
        get
        {
            return _selectedOption_I;
        }
        set
        {
            _selectedOption_I = value;
            if (_selectedOption_I == null)
            {
                optionGroupListUI_II.Clear();
                optionGroupListUI_II.gameObject.SetActive(false);
            }
            else
            {
                optionGroupListUI_II.gameObject.SetActive(true);
            }
        }
    }
    OptionContent_Option _selectedOption_I;
    /// <summary>
    /// 所有的一级选项
    /// </summary>
    public Dictionary<int, OptionContent> optionGroupList = new Dictionary<int, OptionContent>();
    /// <summary>
    /// 当前选择的单元格
    /// </summary>
    public List<OptionCellContent> selectedOptionCellContents = new List<OptionCellContent>();
    public bool activeOptionSystem
    {
        get
        {
            return _activeOptionSystem;
        }
        set
        {
            if (_activeOptionSystem != value)
            {
                if(!value)
                {
                    optionGroupListUI_I.Clear();
                    selectedOptionCellContents.Clear();
                    selectedOption_I = null;
                }
                optionGroupListUI_I.gameObject.SetActive(value);
                if (value)
                {
                    ShowGroupList();
                }
                _activeOptionSystem = value;
            }
        }
    }

    bool _activeOptionSystem = true;
    int maxOptionIndex = 0;
    // Use this for initialization
    void Start () {
        optionGroupListUI_I.addOption.onClick.AddListener(AddOneOption_I);
        optionGroupListUI_II.addOption.onClick.AddListener(AddOneOption_II);
        SheetManager.sheetManager.sheetOperator.onSelectCell += OnSelectCell;
        SheetManager.sheetManager.sheetOperator.onDeselectCell += OnSelectCell;
        activeOptionSystem = false;
        
	}
	

    public void AddOneOption_I()
    {
        maxOptionIndex++;
        OptionContent_Option option_I = new OptionContent_Option();
        option_I.number = maxOptionIndex;
        optionGroupList.Add(maxOptionIndex,option_I);
        OptionUI ou = optionGroupListUI_I.AddOneOptionUI(option_I);
        ou.onSelected += OnSelectOneOption_I;
        ou.onDeselected += OnDeselectOneOption_I;
    }

    public void AddOneOption_I(OptionContent_Option option_I)
    {
        if (option_I.number > maxOptionIndex)
        {
            maxOptionIndex = option_I.number;
        }
        optionGroupList.Add(option_I.number, option_I);
        OptionUI ou = optionGroupListUI_I.AddOneOptionUI(option_I);
        ou.onSelected += OnSelectOneOption_I;
        ou.onDeselected += OnDeselectOneOption_I;
    }

    /// <summary>
    /// 添加二级选项
    /// </summary>
    public void AddOneOption_II()
    {
        OptionContent_String O_II = selectedOption_I.AddOneContent();
        OptionUI ou = optionGroupListUI_II.AddOneOptionUI(O_II);
        ou.onSelected += OnSelectOneOption_II;
        ou.onDeselected += OnDeselectOneOption_II;
    }
    /// <summary>
    /// 添加二级选项
    /// </summary>
    /// <param name="option_II"></param>
    public void AddOneOption_II(OptionContent_String option_II)
    {
        OptionUI ou = optionGroupListUI_II.AddOneOptionUI(option_II);
        ou.onSelected += OnSelectOneOption_II;
        ou.onDeselected += OnDeselectOneOption_II;
    }
    public void ShowGroupList()
    {
        List<OptionContent> OPC = new List<OptionContent>();
        foreach (var item in optionGroupList.Values)
        {
            OPC.Add(item);
        }
        optionGroupListUI_I.ShowOptions(OPC);
    }

    public void OnSelectOneOption_I(OptionContent option)
    {
        OptionContent c = selectedOption_I;
        selectedOption_I = (OptionContent_Option)option;
        List<OptionContent> OPC = new List<OptionContent>();
        foreach (var item in selectedOption_I.content.Values)
        {
            OPC.Add(item);
        }
        optionGroupListUI_II.ShowOptions(OPC);
    }
    public void OnDeselectOneOption_I(OptionContent option)
    {
        if (option == selectedOption_I)
        {
            selectedOption_I = null;
        }
        if (selectedOption_I == null)
        {
            optionGroupListUI_II.Clear();
        }
    }

    public void OnSelectOneOption_II(OptionContent option)
    {
        foreach (var item in selectedOptionCellContents)
        {
            item.SetOption((OptionContent_String)option,true);
        }
    }
    public void OnDeselectOneOption_II(OptionContent option)
    {
        foreach (var item in selectedOptionCellContents)
        {
            item.SetOption((OptionContent_String)option,false);
        }
    }
    /// <summary>
    /// 检查当前选择的单元格
    /// </summary>
    public void CheckOptionCellContent()
    {
        selectedOptionCellContents.Clear();
        Cell[] cs = SheetManager.sheetManager.sheetOperator.GetSelectedCells();
        if (cs.Length > 0)
        {
            foreach (var item in cs)
            {
                if (!(((ContentCell)item).cellContent is OptionCellContent))
                {
                    activeOptionSystem = false;
                    return;
                }
                selectedOptionCellContents.Add((OptionCellContent)((ContentCell)item).cellContent);
            }
            activeOptionSystem = true;
        }
        else
        {
            activeOptionSystem = false;
            return;
        }

        #region//单元格中内容只能单选时采用此方法判断当前应该展示的选项框
        //OptionContent_String v = selectedOptionCellContents[0].Values[0];
        //if (v != null)
        //{
        //    bool same = true;
        //    foreach (var item in selectedOptionCellContents)
        //    {
        //        if (item.Value == v)
        //        {
        //            continue;
        //        }
        //        same = false;
        //        break;
        //    }
        //    if (same)
        //    {
        //        optionGroupListUI_I.optionDic[v.myOption].toggle.isOn = true;
        //        optionGroupListUI_II.optionDic[v].toggle.isOn = true;
        //    }
        //    else
        //    {
        //        optionGroupListUI_I.optionDic[selectedOption_I].toggle.isOn = false;
        //    }
        //}
        //else if (selectedOption_I != null)
        //{
        //    optionGroupListUI_I.optionDic[selectedOption_I].toggle.isOn = false;
        //}
        #endregion
        #region//单元格中内容可以多选时采用此方法判断当前应该展示的选项框
        if (selectedOptionCellContents.Count > 1 && selectedOptionCellContents[0].Values.Count != 0)
        {
            bool same = true;
            for (int i = 1; i < selectedOptionCellContents.Count; i++)
            {
                if (!same)
                {
                    break;
                }
                if (selectedOptionCellContents[i].Values.Count != selectedOptionCellContents[0].Values.Count)
                {
                    same = false;
                    break;
                }
                else
                {
                    List<OptionContent_String> os = new List<OptionContent_String>();
                    os.AddRange(selectedOptionCellContents[0].Values);
                    for (int j = 0; j < selectedOptionCellContents[0].Values.Count; j++)
                    {
                        if (os.Contains(selectedOptionCellContents[i].Values[j]))
                        {
                            os.Remove(selectedOptionCellContents[i].Values[j]);
                            continue;
                        }
                    }
                    if (os.Count > 0)
                    {
                        same = false;
                        break;
                    }
                    //if( selectedOptionCellContents[0].Values[j] == selectedOptionCellContents[i].Values[j])
                    //{
                    //    continue;
                    //}
                }
            }
            if (same)
            {
                optionGroupListUI_I.optionDic[selectedOptionCellContents[0].Values[0].myOption].toggle.isOn = true;
                foreach (var item in selectedOptionCellContents[0].Values)
                {
                    optionGroupListUI_II.optionDic[item].toggle.isOn = true;
                }
            }
            else
            {
                optionGroupListUI_I.optionDic[selectedOption_I].toggle.isOn = false;
            }
        }
        else if(selectedOptionCellContents.Count == 1 && selectedOptionCellContents[0].Values.Count != 0)
        {
            optionGroupListUI_I.optionDic[selectedOptionCellContents[0].Values[0].myOption].toggle.isOn = true;
            foreach (var item in selectedOptionCellContents[0].Values)
            {
                optionGroupListUI_II.optionDic[item].toggle.isOn = true;
            }
        }
        else if(selectedOption_I != null)
        {
            optionGroupListUI_I.optionDic[selectedOption_I].toggle.isOn = false;
        }
        #endregion
    }

    void OnSelectCell(Cell cell)
    {
        CheckOptionCellContent();
    }
}
